import axios from 'axios'
import router from "@/router";
let noToken = ["/retailers-user-server/api/login/token"]
import {setToken,getToken} from './auth'
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建axios实例gg
const service = axios.create({
    // axios中请求配置有baseURL选项，表示请求URL公共部分
    baseURL: '/',
    // 超时
    timeout: 10000,
})

// request拦截器
service.interceptors.request.use(
    config => {//配置请求
        // 登陆时将响应的令牌set进去
        // setToken("Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ7XCJhY2NvdW50XCI6XCJ6aGFuZ3NhblwifSIsImV4cCI6MTY0OTMwNjA0NH0.9chsmaLcMhvQHxjTQu2wacRnmH72N1z1RW-uevFQur1wnwnkKdvJwHc8jQzQIAnzL-b8St23PdW-H3EIwvHMuw");
        if(noToken.includes(config.url)){
            console.log(config,'config')
            return config
        }else{
            if (getToken()) { //获取token，后续解析
                config.headers['Authorization'] =getToken();
            }else{
                router.push({path:'/login'})
            }
            return config
        }
    },
    error => {
        return Promise.reject(error)
    }
)

// response 拦截器
service.interceptors.response.use(
    response => {
      const res = response.data
      if (res.code !== 200) { // 后台返回码，根据自己的业务进行修改
          this.$message({
              message: res.msg,
              type: 'error'
          })

      } else {
        return response
      }
    },
    error => {
      return Promise.reject(error)
    }
)
export default service
